package com.ambulance.project.manager.service.impl;

import com.ambulance.common.constant.Constants;
import com.ambulance.common.exception.CustomException;
import com.ambulance.common.utils.SecurityUtils;
import com.ambulance.project.manager.domain.BizDispatcher;
import com.ambulance.project.manager.mapper.BizDispatcherMapper;
import com.ambulance.project.manager.service.IBizDispatcherService;
import com.ambulance.project.manager.utils.MgtCommonUtil;
import com.ambulance.project.manager.utils.MgtUserLoginResult;
import com.ambulance.project.system.domain.SysUser;
import com.ambulance.project.system.service.ISysUserService;
import com.ambulance.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 调度员管理Service业务层处理
 *
 * @author zr
 * @date 2020-05-11
 */
@Service
public class BizDispatcherServiceImpl implements IBizDispatcherService {
    @Autowired
    private BizDispatcherMapper bizDispatcherMapper;
    @Autowired
    private MgtCommonUtil mgtCommonUtil;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询调度员管理
     *
     * @param dispatcherID 调度员管理ID
     * @return 调度员管理
     */
    @Override
    public BizDispatcher selectBizDispatcherById(Long dispatcherID) {
        BizDispatcher bizDispatcher = bizDispatcherMapper.selectBizDispatcherById(dispatcherID);
        bizDispatcher.setPassword(Constants.PASSWORD_MARK);
        return bizDispatcher;
    }

    /**
     * 查询调度员管理列表
     *
     * @param bizDispatcher 调度员管理
     * @return 调度员管理
     */
    @Override
    public List<BizDispatcher> selectBizDispatcherList(BizDispatcher bizDispatcher) {
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizDispatcher.setUid(new Long(mgtUserLoginResult.getUid()));
        return bizDispatcherMapper.selectBizDispatcherList(bizDispatcher);
    }

    /**
     * 新增调度员管理
     *
     * @param bizDispatcher 调度员管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizDispatcher(BizDispatcher bizDispatcher) {

        //新增调度员的时候同时插入登陆者表
        SysUser sysUser = new SysUser();
        MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        sysUser.setNickName(bizDispatcher.getName());
        sysUser.setPassword((SecurityUtils.encryptPassword(bizDispatcher.getPassword())));
        sysUser.setUserName(bizDispatcher.getAccountName());
        SysUser sysUser1 = iSysUserService.selectUserByUserName(bizDispatcher.getAccountName());
        if (!CommonUtils.checkNull(sysUser1)) {
            throw new CustomException("账户名已存在");
        }
        // 调度员角色ID 100
        sysUser.setRole(100);
        //插入登陆者表
        iSysUserService.insertUser(sysUser);
        bizDispatcher.setHospital(mgtUserLoginResult.getHospitalName());
        bizDispatcher.setCid(sysUser.getUserId());
        bizDispatcher.setUid(new Long(mgtUserLoginResult.getUid()));
        return bizDispatcherMapper.insertBizDispatcher(bizDispatcher);
    }

    /**
     * 修改调度员管理
     *
     * @param bizDispatcher 调度员管理
     * @return 结果
     */
    @Override
    public int updateBizDispatcher(BizDispatcher bizDispatcher) {
        String password = bizDispatcher.getPassword();
        if (Constants.PASSWORD_MARK.equals(password)) {
            bizDispatcher.setPassword(null);
        }
        return bizDispatcherMapper.updateBizDispatcher(bizDispatcher);
    }

    /**
     * 批量删除调度员管理
     *
     * @param dispatcherIDs 需要删除的调度员管理ID
     * @return 结果
     */
    @Override
    public int deleteBizDispatcherByIds(Long[] dispatcherIDs) {
        return bizDispatcherMapper.deleteBizDispatcherByIds(dispatcherIDs);
    }

    /**
     * 删除调度员管理信息
     *
     * @param dispatcherID 调度员管理ID
     * @return 结果
     */
    @Override
    public int deleteBizDispatcherById(Long dispatcherID) {
        return bizDispatcherMapper.deleteBizDispatcherById(dispatcherID);
    }
}
